<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Start action switch
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  Add events case
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Check if he has acces
		//-----------------------------------------------------------------

		if(isset($is_loged_in))
		{
			//-----------------------------------------------------------------
			//  BBcode check
			//-----------------------------------------------------------------

			for ($i = 1; $i <= 5; $i++) if(check_tag_order($sendmessage) == $i) { header("location: $PHP_SELF?n=modules/events&a=4&d=$eventdaya&m=$eventmontha&i=9$i"); exit; }

			//-----------------------------------------------------------------
			//  Make time for start and end
			//-----------------------------------------------------------------

			if($eventstartah == 0 || $eventstartam == 0 || $eventendah == 0 || $eventendam == 0)
			{
				$eventstarta = 0;
				$eventenda = 0;
			}
			else
			{
				if($eventstartam == 1) $eventstartam = 0;
				if($eventstartam == 2) $eventstartam = 15;
				if($eventstartam == 3) $eventstartam = 30;
				if($eventstartam == 4) $eventstartam = 45;

				if($eventendam == 1) $eventendam = 0;
				if($eventendam == 2) $eventendam = 15;
				if($eventendam == 3) $eventendam = 30;
				if($eventendam == 4) $eventendam = 45;

				$eventstarta = mktime($eventstartah, $eventstartam, 0, $eventmontha, $eventdaya, $eventyeara);
				$eventenda = mktime($eventendah, $eventendam, 0, $eventmontha, $eventdaya, $eventyeara);
			}

			//-----------------------------------------------------------------
			//  Take care of some things
			//-----------------------------------------------------------------

			$sendmessage = jcm_htmlspecialchars(jcm_linebreaks(trim(jcm_stripslashes($sendmessage))));
			if($overflow = check_tag_order($sendmessage)) $sendmessage = substr($sendmessage, 0, $overflow[0]).substr($sendmessage, $overflow[1], (strlen($sendmessage) - $overflow[0]));

			//-----------------------------------------------------------------
			//  Add in database
			//-----------------------------------------------------------------

			$db->insertRow("events",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','day' => ''.$db->escape($eventdaya).'','month' => ''.$db->escape($eventmontha).'','year' => ''.$db->escape($eventyeara).'','start' => ''.$db->escape($eventstarta).'','end' => ''.$db->escape($eventenda).'','message' => ''.$db->escape($sendmessage).''));

			//-----------------------------------------------------------------
			//  Redirect when finished
			//-----------------------------------------------------------------

			header("location: $PHP_SELF?n=modules/events&d=$eventdaya&m=$eventmontha&i=44");
		}

	break;

	//-----------------------------------------------------------------
	//  Edit events case
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Check if he has acces
		//-----------------------------------------------------------------

		if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster))
		{
			//-----------------------------------------------------------------
			//  BBcode check
			//-----------------------------------------------------------------

			for ($i = 1; $i <= 5; $i++) if(check_tag_order($sendmessage) == $i) { header("location: $PHP_SELF?n=modules/events&a=5&d=$eventdaye&m=$eventmonthe&o=$o&i=9$i"); exit; }

			//-----------------------------------------------------------------
			//  Make time for start and end
			//-----------------------------------------------------------------

			if($eventstarteh == 0 || $eventstartem == 0 || $eventendeh == 0 || $eventendem == 0)
			{
				$eventstarte = 0;
				$eventende = 0;
			}
			else
			{
				if($eventstartem == 1) $eventstartem = 0;
				if($eventstartem == 2) $eventstartem = 15;
				if($eventstartem == 3) $eventstartem = 30;
				if($eventstartem == 4) $eventstartem = 45;

				if($eventendem == 1) $eventendem = 0;
				if($eventendem == 2) $eventendem = 15;
				if($eventendem == 3) $eventendem = 30;
				if($eventendem == 4) $eventendem = 45;

				$eventstarte = mktime($eventstarteh, $eventstartem, 0, $eventmonthe, $eventdaye, $eventyeare);
				$eventende = mktime($eventendeh, $eventendem, 0, $eventmonthe, $eventdaye, $eventyeare);
			}

			//-----------------------------------------------------------------
			//  Take care of some things
			//-----------------------------------------------------------------

			$sendmessage = jcm_linebreaks(trim(jcm_stripslashes($sendmessage)));
			if($overflow = check_tag_order($sendmessage)) $sendmessage = substr($sendmessage, 0, $overflow[0]).substr($sendmessage, $overflow[1], (strlen($sendmessage) - $overflow[0]));

			//-----------------------------------------------------------------
			//  Update the database
			//-----------------------------------------------------------------

			$db->updateRow("events",array('day' => ''.$db->escape($eventdaye).'','month' => ''.$db->escape($eventmonthe).'','year' => ''.$db->escape($eventyeare).'','start' => ''.$db->escape($eventstarte).'','end' => ''.$db->escape($eventende).'','message' => ''.$db->escape($sendmessage).''),"id = ".$db->escape($o)."");

			//-----------------------------------------------------------------
			//  Redirect when finished
			//-----------------------------------------------------------------

			header("location: $PHP_SELF?n=modules/events&d=$eventdaye&m=$eventmonthe&i=45");
		}

	break;

	//-----------------------------------------------------------------
	//  Delete events case
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Check if he has acces
		//-----------------------------------------------------------------

		if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster))
		{
			//-----------------------------------------------------------------
			//  Delete from the database
			//-----------------------------------------------------------------

			$db->deleteRow("events","id = '".$db->escape($o)."'");

			//-----------------------------------------------------------------
			//  Redirect when finished
			//-----------------------------------------------------------------

			header("location: $PHP_SELF?n=modules/events&d=$d&m=$m&i=46");
		}

	break;

	//-----------------------------------------------------------------
	//  View Form for adding events case
	//-----------------------------------------------------------------

	case 4:

		//-----------------------------------------------------------------
		//  Check if he has acces
		//-----------------------------------------------------------------

		if(isset($is_loged_in))
		{
			?>
				<form method="post" action="<?= $PHP_SELF ?>?n=modules/events" name="addevent">
				<table class="main" cellspacing="1" cellpadding="4" width="100%">
				<col width="25%"><col width="75%">
				<tr class="head"> 
				<td colspan="2" class="head"><?= $language['Events add/edit title'] ?></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><?= $language['Events add/edit desc'] ?></td>
				<td class="con1" valign="bottom"><?= $language['Events add/edit desc2'] ?> 
				<select name="eventdaya" class="box" style="width:14%">
			<?
			for ($i = 1; $i <= 31; $i++)
			{
				if(isset($d))
				{
					if($i == $d) $selected = "selected";
					else $selected = "";
				}

				echo "<option value='$i' $selected>$i</option>\n";
			}
			?>
				</select> <?= $language['Events add/edit desc3'] ?> <select name="eventmontha" class="box" style="width:32%">
			<?
			for ($i = 1; $i <= 12; $i++)
			{
				if(isset($m))
				{
					if($i == $m) $selected = "selected";
					else $selected = "";
				}

				echo "<option value='$i' $selected>{$months[$i]}</option>\n";
			}
			?>
				</select> <?= $language['Events add/edit desc4'] ?> <select name="eventyeara" class="box" style="width:26%">
			<?
			for ($i = date("Y"); $i <= date("Y"); $i++)
			{
				if($i == date('Y')) $selected = "selected";
				else $selected = "";

				echo "<option value='$i' $selected>$i</option>\n";
			}
			?>
				</select>
				<tr>
				<td class="con1" valign="middle"><?= $language['Events add/edit desc5'] ?></td>
				<td class="con1" valign="bottom"><?= $language['Events add/edit desc6'] ?> <select name="eventstartah" class="box" style="width:45px"><option value="0" selected></option><? for($i = 1; $i <= 24; $i++){ echo "<option value='$i'>$i</option>\n"; } ?></select>:<select name="eventstartam" class="box" style="width:45px"><option value="0" selected></option><option value="1">00</option><option value="2">15</option><option value="3">30</option><option value="4">45</option></select> / <select name="eventendah" class="box" style="width:45px"><option value="0" selected></option><? for($i = 1; $i <= 24; $i++){ echo "<option value='$i'>$i</option>\n"; } ?></select>:<select name="eventendam" class="box" style="width:45px"><option value="0" selected></option><option value="1">00</option><option value="2">15</option><option value="3">30</option><option value="4">45</option></select> <?= $language['Events add/edit desc7'] ?></td>
				</tr>
				<tr>
				<td class="con1" valign="top"><?= "{$language['Events add/edit desc8']}".dis_bb_em()."" ?></td>
				<td class="con1" valign="middle"><textarea style="width:100%; height:70px;" class="box" name="sendmessage" tabindex="6"></textarea></td>
				</tr>
				<tr>
				<td class="con1"><input type="button" style="width:100" class="box" value="<?= $language['Events add/edit desc9'] ?>" onClick="window.history.go(-1);" tabindex="13"></td>
				<td class="con1" align="right"><input type="submit" style="width:100" class="box" value="<?= $language['Events add/edit desc10'] ?>" tabindex="12"></td>
				</tr>
				<input type="hidden" name="a" value="1">
				</table></form>
			<?
		}

	break;

	//-----------------------------------------------------------------
	//  View Form for editing events case
	//-----------------------------------------------------------------

	case 5:

		//-----------------------------------------------------------------
		//  Check if he has acces
		//-----------------------------------------------------------------

		if(isset($is_loged_in))
		{
			//-----------------------------------------------------------------
			//  Look into da database for da event
			//-----------------------------------------------------------------
			
			$event_info = $db->getLine("SELECT * FROM events WHERE id = '".$db->escape($o)."'") ?>
				<form method="post" action="<?= $PHP_SELF ?>?n=modules/events" name="editevent">
				<table class="main" cellspacing="1" cellpadding="4" width="100%">
				<col width="25%"><col width="75%">
				<tr class="head"> 
				<td colspan="2" class="head"><?= $language['Events add/edit title2'] ?></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><?= $language['Events add/edit desc'] ?></td>
				<td class="con1" valign="bottom"><?= $language['Events add/edit desc2'] ?> 
				<select name="eventdaye" class="box" style="width:14%">
			<?
			for ($i = 1; $i <= 31; $i++)
			{
				if(isset($d))
				{
					if($i == $d) $selected = "selected";
					else $selected = "";
				}

				echo "<option value='$i' $selected>$i</option>\n";
			}
			?>
				</select> <?= $language['Events add/edit desc3'] ?> <select name="eventmonthe" class="box" style="width:32%">
			<?
			for ($i = 1; $i <= 12; $i++)
			{
				if(isset($m))
				{
					if($i == $m) $selected = "selected";
					else $selected = "";
				}

				echo "<option value='$i' $selected>{$months[$i]}</option>\n";
			}
			?>
				</select> <?= $language['Events add/edit desc4'] ?> <select name="eventyeare" class="box" style="width:26%">
			<?
			for ($i = date("Y"); $i <= date("Y"); $i++)
			{
				if($i == date('Y')) $selected = "selected";
				else $selected = "";

				echo "<option value='$i' $selected>$i</option>\n";
			}
			?>
				</select>
				<tr>
				<td class="con1" valign="middle"><?= $language['Events add/edit desc5'] ?></td>
				<td class="con1" valign="bottom"><?= $language['Events add/edit desc6'] ?> <select name="eventstarteh" class="box" style="width:45px">
			<?
			if($event_info['start'] != 0)
				echo "<option value='0'></option>";
			else
				echo "<option value='0' selected></option>";

			for($i = 1; $i <= 24; $i++)
			{
				if($event_info['start'] != 0)
				{
					if($i == date("H",$event_info['start'])) $selected = "selected";
					else $selected = "";

					echo "<option value='$i' $selected>$i</option>\n";
				}
				else
					echo "<option value='$i'>$i</option>\n";
			}
			?>
				</select>:<select name="eventstartem" class="box" style="width:45px">
			<?
			if($event_info['start'] != 0)
			{
				if(date("i",$event_info['start']) == 0) echo "<option value='0' selected></option>"; else echo "<option value='0'></option>";
				if(date("i",$event_info['start']) == 00) echo "<option value='1' selected>00</option>"; else echo "<option value='1'>00</option>";
				if(date("i",$event_info['start']) == 15) echo "<option value='2' selected>15</option>"; else echo "<option value='2'>15</option>";
				if(date("i",$event_info['start']) == 30) echo "<option value='3' selected>30</option>"; else echo "<option value='3'>30</option>";
				if(date("i",$event_info['start']) == 45) echo "<option value='4' selected>45</option>"; else echo "<option value='4'>45</option>";
			}
			else echo "<option value='0' selected></option><option value='1'>00</option><option value='2'>15</option><option value='3'>30</option><option value='4'>45</option>" ?>
				</select> / <select name="eventendeh" class="box" style="width:45px">
			<?
			if($event_info['end'] != 0)
				echo "<option value='0'></option>";
			else
				echo "<option value='0' selected></option>";

			for($i = 1; $i <= 24; $i++)
			{
				if($event_info['end'] != 0)
				{
					if($i == date("H",$event_info['end'])) $selected = "selected";
					else $selected = "";

					echo "<option value='$i' $selected>$i</option>\n";
				}
				else
					echo "<option value='$i'>$i</option>\n";
			}
			?>
				</select>:<select name="eventendem" class="box" style="width:45px">
			<?
			if($event_info['end'] != 0)
			{
				if(date("i",$event_info['end']) == 0) echo "<option value='0' selected></option>"; else echo "<option value='0'></option>";
				if(date("i",$event_info['end']) == 00) echo "<option value='1' selected>00</option>"; else echo "<option value='1'>00</option>";
				if(date("i",$event_info['end']) == 15) echo "<option value='2' selected>15</option>"; else echo "<option value='2'>15</option>";
				if(date("i",$event_info['end']) == 30) echo "<option value='3' selected>30</option>"; else echo "<option value='3'>30</option>";
				if(date("i",$event_info['end']) == 45) echo "<option value='4' selected>45</option>"; else echo "<option value='4'>45</option>";
			}
			else echo "<option value='0' selected></option><option value='1'>00</option><option value='2'>15</option><option value='3'>30</option><option value='4'>45</option>" ?>
				</select> <?= $language['Events add/edit desc7'] ?></td>
				</tr>
				<tr>
				<td class="con1" valign="top"><?= "{$language['Events add/edit desc8']}".dis_bb_em()."" ?></td>
				<td class="con1" valign="bottom"><textarea style="width:100%; height:70px;" class="box" name="sendmessage" tabindex="6"><?= $event_info['message'] ?></textarea></td>
				</tr>
				<tr>
				<td class="con1"><input type="button" style="width:100" class="box" value="<?= $language['Events add/edit desc9'] ?>" onClick="window.history.go(-1);" tabindex="8"></td>
				<td class="con1" align="right"><input type="submit" style="width:100" class="box" value="<?= $language['Events add/edit desc11'] ?>" tabindex="7"></td>
				</tr>
				<input type="hidden" name="a" value="2">
				<input type="hidden" name="o" value="<?= $event_info['id'] ?>">
				</table></form>
			<?
		}

	break;

	//-----------------------------------------------------------------
	//  View events default case
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Load the events row
		//-----------------------------------------------------------------

		$events_db = $db->getTable("SELECT e.id, e.type, e.user_id, e.user_user, e.start, e.day, e.month, e.year, e.end, e.message, u.age FROM events AS e LEFT JOIN users AS u ON e.user_id = u.id WHERE day = '".$db->escape($d)."' AND month = '".$db->escape($m)."' ORDER BY start ASC,type DESC");

		foreach($events_db as $idx=>$event)
		{
			//-----------------------------------------------------------------
			//  Figure out the start and end
			//-----------------------------------------------------------------

			if($event['type'] == "normal")
			{
				if($event['year'] == date('Y'))
				{
					//-----------------------------------------------------------------
					//  Code Layout
					//-----------------------------------------------------------------

					if($event['start'] != 0 || $event['end'] != 0) $layout .= "<tr><td valign='top' class='con1'>".date("H:i",$event['start'])." - ".date("H:i",$event['end'])."</td>";
					else $layout .= "<tr><td valign='top' class='con1'>{$language['Events view desc2']}</td>";
				}
				else $layout .= NULL;
			}
			else $layout .= "<tr><td class='con1'>{$language['Events view desc2']}</td>";

			//-----------------------------------------------------------------
			//  Figure out if it's a bday or just event
			//-----------------------------------------------------------------

			if($event['type'] == "normal")
			{
				if($event['year'] == date('Y'))
				{
					//-----------------------------------------------------------------
					//  Code Layout
					//-----------------------------------------------------------------

					if(isset($event['age'])) $layout .= "<td class='con1'><a href='$PHP_SELF?n=modules/users&a=1&d={$event['user_id']}'>{$event['user_user']}</a>: ".parse_message($event['message'])."</td>";
					else $layout .= "<td class='con1'>{$event['user_user']}: ".parse_message($event['message'])."</td>";

					if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster)) $layout .= "<td valign='bottom' class='con1'><a href='$PHP_SELF?n=modules/events&a=5&d=$d&m=$m&o={$event['id']}'><img src='templates/$template/images/buttons/edit.gif' border='0' alt='{$language['Events view desc8']}'></a><a href='$PHP_SELF?n=modules/events&a=3&d=$d&m=$m&o={$event['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Events view desc9']}'></a></td></tr>\n"; else $layout .= "</tr>\n";
				}
				else $layout .= NULL;
			}
			else
			{
				//-----------------------------------------------------------------
				//  Calculate Age
				//-----------------------------------------------------------------

				if($event['day'] > date("j") || $event['month'] > date("n"))
				{
					$surplus = (date("Y") + 1) - date("Y");
					$bday_age = find_age("{$event['age']}", $surplus);
				}
				else
					$bday_age = find_age("{$event['age']}");

				//-----------------------------------------------------------------
				//  Calculate Time for bday
				//-----------------------------------------------------------------

				if($event['day'] == date("j") && $event['month'] == date("n"))
				{
					$bday_date_1 = $language['Events view desc3'];
					$bday_date_2 = $language['Events view desc4'];
					$bday_date_3 = $language['Events view desc5'];
				}
				else
				{
					$bday_date_1 = NULL;
					$bday_date_2 = $language['Events view desc6'];
					$bday_date_3 = "{$language['Events view desc7']} {$event['day']}-{$months[$event['month']]}";
				}

				//-----------------------------------------------------------------
				//  Code Layout
				//-----------------------------------------------------------------

				$layout .= "<td class='con1'>$bday_date_1 <a href='$PHP_SELF?n=modules/users&a=1&d={$event['user_id']}'>{$event['user_user']}</a> $bday_date_2 $bday_age $bday_date_3</td>\n";

				if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster)) $layout .= "<td valign='bottom' class='con1'><img src='templates/$template/images/buttons/noedit.gif' border='0' alt='{$language['Events view desc10']}'><img src='templates/$template/images/buttons/nodelete.gif' border='0' alt='{$language['Events view desc11']}'></td></tr>"; else $layout .= "</tr>";
			}
		}

		//-----------------------------------------------------------------
		//  If no event that day, show error
		//-----------------------------------------------------------------

		if($events_db == FALSE) $layout .= "<tr><td class='con1' colspan='3'>{$language['Events view desc']}</td></tr>";

		//-----------------------------------------------------------------
		//  Start Layout
		//-----------------------------------------------------------------

		?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='4'><?= $language['Events view title'] ?></td></tr>
			<tr class="bottom">
			<td width="17%"><?= $language['Events view title2'] ?></td>
			<td width="82%"><?= $language['Events view title3'] ?></td>
		<? if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster)){ echo "<td width='1%'>&nbsp;</td>"; } ?>
			</tr>
			<?= $layout ?>
			</table>
		<?
}

?>